<?php

namespace App\Model;

use PhalApi\Model\DataModel;
use PhalApi\Cache;
use PhalApi\Model\NotORMModel as NotORM;
use App\Model\IdcardInfo as Info;
use App\Model\IdcardFromUnitCheck as FromUnit;
use App\Model\CutDownTimes as CutDownTimes;
use App\Model\IdcardWater as IdcardWater;


class FcjdHuajing extends DataModel
{
    public $tableName = 'idcard_check_list';
    protected function getTableName($id)
    {
        return $this->tableName;
    }

    function getGenderFromIdCard($idCard) {
        // 判断身份证号长度
        if (strlen($idCard) != 18) {
            return '非法身份证号';
        }
        // 获取身份证号倒数第二位的数字
        $genderNum = (int)substr($idCard, -2, 1);
        // 判断性别
        if ($genderNum % 2 == 1) {
            return '男';
        } else {
            return '女';
        }
    }

    //存入数据
    public function checkIdCard_FcjdMahine($data)
    {

        if (is_array($data['spectrum'])) {
            $data['spectrum'] = json_encode($data['spectrum']);
        }
        if( $data['IdCard']){
            $data['Sex'] = $this->getGenderFromIdCard($data['IdCard']);
        }
        // $data['Sex'] = $this->getGenderFromIdCard($data['IdCard']);

        $curl = new \PhalApi\CUrl();
        // 设置请求的头部信息。
        $curl->setHeader(array('Content-Type' => 'application/json'));
        $url = 'https://oapi.dingtalk.com/robot/send?access_token=c230bf201cf694db851c14413af45f3c216754466886f7f46647eb3678d809a4';
        $dingdingdata = array(
            'msgtype' => 'text',
            'text' => array(
                "content" => "通知：" .$data['MachineCode'] . "-" . $data['SerialNumber'] . $data['SubProjectName']
            )
        );
        $url2 = 'https://oapi.dingtalk.com/robot/send?access_token=09d6f6a9ba294b1e5bf1452d73e47a00b213f8dc7b6991df7c8720b79ec8046c';

        // O6-单乙酰吗啡、吗啡、冰毒、苯丙胺、MDA、MDMA、氯胺酮、去甲氯胺酮、甲卡西酮、可卡因、苯甲酰爱康宁、四氢大麻酚、芬太尼、美沙酮、曲马多、氟胺酮、芬太尼、甲卡西酮、氟硝西帕、依托咪酯
        $curl->post($url, json_encode($dingdingdata), 60000);
        $curl->post($url2, json_encode($dingdingdata), 60000);
        // return '';
    
        // 读出样本号
        // $ids = $data['SampleNo'];
        // //到数据库里查
        // if( $ids ){
        //     $this->tableName = 'idcard_check_list';
        //     $hasid = $this->getORM()->select('id')->where('id', $ids)->fetchOne();
        //     if($hasid){
        //         $data['ids'] = $hasid['id'];
        //         $data['create_time'] = date('Y-m-d H:i:s', time());
        //         $this->tableName = 'idcard_ms_result';
        //         return $this->getORM()->insert($data);
        //     }
        //     if( $data['IdCard'] ){
        //         $hasid = $this->getORM()->select('id')->where('IdNum', $data['IdCard'])->order('id DESC')->fetchOne();
        //         if( $hasid ){
        //             $data['ids'] = $hasid['id'];
        //             $data['create_time'] = date('Y-m-d H:i:s', time());
        //             $this->tableName = 'idcard_ms_result';
        //             return $this->getORM()->insert($data);
        //         }
        //     }
        // }

        // $hasRrecod = \PhalApi\DI()->cache->get($data['SerialNumber'].$data['SubProjectName']);
        // if( $hasRrecod ){
        //     return $hasRrecod;
        // }else{
        //     \PhalApi\DI()->cache->set($data['SerialNumber'].$data['SubProjectName'], $data['SerialNumber'], 3600);
        // }

        $checkRecrod = array(
            'SerialNumber' => $data['SerialNumber'],
            'SubProjectName' => $data['SubProjectName'],
        );

        \PhalApi\DI()->logger->debug('checkRecrod', $data);
        
        $this->tableName = 'idcard_ms_result';
        $hasRreod = $this->getORM()->select('id')->where($checkRecrod)->fetchOne();
        if( $hasRreod ){
            $this->getORM()->select('id')->where('id', $hasRreod['id'])->update($data);
            \PhalApi\DI()->logger->debug('checkRecrod', $hasRreod);
            return $data;
        }
        $data['create_time'] = date('Y-m-d H:i:s', time());
        return $this->getORM()->insert($data);
    }
}
